package com.xgjweb.common.utils;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import sun.misc.BASE64Encoder;

import com.xgjweb.common.action.XgjWebForm;

/**
 * 工具类
 * 
 * @author 谢国俊 作用： 日期：Oct 31, 2009
 */
public final class XgjWebUtils {
	private XgjWebUtils() {

	}

	public static final Integer PREV_MONTH = -1;
	public static final Integer NOW_MONTH = 0;
	public static final Integer NEXT_MONTH = 1;
	private static final SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
			"yyyy-MM-dd HH:mm:ss");

	/**
	 * 加密
	 * 
	 * @param str
	 * @return
	 * @throws NoSuchAlgorithmException
	 * @throws UnsupportedEncodingException
	 */
	public static String processPwd(String str)
			throws NoSuchAlgorithmException, UnsupportedEncodingException {
		MessageDigest md5 = MessageDigest.getInstance("MD5");
		BASE64Encoder base64en = new BASE64Encoder();
		String newstr = base64en.encode(md5.digest(str.getBytes("utf-8")));
		return newstr;
	}

	/**
	 * 替换空格
	 * 
	 * @param s
	 * @return
	 */
	public static String replaceBlank(String s) {
		Pattern p = Pattern.compile("\\s*|\t|\r|\n");
		Matcher m = p.matcher(s);
		return m.replaceAll("");
	}

	/**
	 * 返回数组处理后的以逗号分隔数组元素的字符串
	 * 
	 * @param strs
	 * @return
	 */
	public static String getStringByComma(String[] strs) {
		if (null != strs && strs.length > 0) {

			List<String> list = Arrays.asList(strs);
			String s = list.toString();
			s = s.substring(1, s.length() - 1);
			return replaceBlank(s);
		} else
			return null;
	}

	/**
	 * 设置某个月的第一天和最大一天
	 */
	public static void setDateByNowMonth(XgjWebForm jwForm, Integer month_offset) {
		Calendar calendar = Calendar.getInstance();
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		calendar.add(Calendar.MONTH, month_offset);
		calendar.set(calendar.DAY_OF_MONTH, calendar
				.getActualMinimum(Calendar.DAY_OF_MONTH)); // 最小的一天
		jwForm.setStart_date(calendar.getTime());
		calendar.set(calendar.DAY_OF_MONTH, calendar
				.getActualMaximum(Calendar.DAY_OF_MONTH)); // 最大的一天
		jwForm.setEnd_date(calendar.getTime());
	}

	/**
	 * 返回uuid
	 * 
	 * @return
	 */
	public static String getUUID() {
		UUID uuid = UUID.randomUUID();
		return uuid.toString();
	}

	/**
	 * 把字符串日期形式格式化
	 * 
	 * @param dateStr
	 * @return
	 * @throws Exception
	 */
	public static String dateFormatStr(String dateStr) {
		if(dateStr==null)
			return null;
		try {
			return simpleDateFormat.format(simpleDateFormat.parse(dateStr));
		} catch (Exception e) {
			XgjWebLogger.getInstance().error(e.getMessage(), e);
		}
		return simpleDateFormat.format(new Date());
	}

	/**
	 * 把日期形式格式化
	 * 
	 * @param dateStr
	 * @return
	 * @throws Exception
	 */
	public static String dateFormat(Date date)   {
		if(date==null)
			return null;
		try {
			return simpleDateFormat.format(date);
		} catch (Exception e) {
			XgjWebLogger.getInstance().error(e.getMessage(), e);
		}
		return simpleDateFormat.format(new Date());
	}

	public static void main(String[] args) {
		System.out.println(getUUID());
	}

}
